package ru.mipt.board.Analysis
import ru.mipt.board.DB._
import ru.mipt.board.net._
import scala.collection.mutable.{HashMap}
import org.squeryl.PrimitiveTypeMode._
import org.squeryl._
import org.squeryl.dsl.ast.{LogicalBoolean}
/**
 * Created by IntelliJ IDEA.
 * User: IL
 * Date: 29.10.10
 * Time: 17:20
 * To change this template use File | Settings | File Templates.
 */


class User(name :String,vector :Array[Double]){}
case class Word(value :String,frequency :Int ){}
case class UserWord(value :String,frequency :Int, username: String){}


class Analyst extends DBuser  {

    val separators = Array(' ', ',', ':', ';', '.', '(', ')', '!', '?', '=', '[', ']')
    val maxlength = 20
    val minlength = 2
    val minfrequency = 2

   /* def postToWords(ID: Int) = {
        inDB{
            val words = new HashMap[String, Int]
            val post = from(testdb.MESSAGES)(s => where(s.ID === ID) select(s))
            for {word <- post.head.text.split(separators)
                 baseword = Porter.Stem(word)
                 if( ! baseword.isEmpty)
                 if(baseword.length < 255)
            }
            {
                val cur_keyword = from(asd.KeyWords2)(s => where (s.value === baseword) select(s))
                if (cur_keyword.headOption.isEmpty) asd.KeyWords2.insert(new Word(baseword,1))
                else update(asd.KeyWords2)(s => where(s.value === baseword) set(s.value := baseword, s.frequency := (s.frequency.~ + 1) ))
            }
        }
    }  */

    def userwords(username: String)={
      inDB{
        val posts = from(testdb.MESSAGES)(s => where(s.username === username) select(s))
        for {post <- posts
             word <- post.text.split(separators)
             baseword = Porter.Stem(word)
             if( ! baseword.isEmpty)
             if(baseword.length < 250)
        } {
             val cur_keyword = from(asd.KeyWords3)(s => where (s.value === baseword and s.username === username) select(s))
                if (cur_keyword.headOption.isEmpty) asd.KeyWords3.insert(new UserWord(baseword,1,username))
                else update(asd.KeyWords3)(s => where(s.value === baseword) set(s.value := baseword, s.frequency := (s.frequency.~ + 1),s.username := username))
            }
        }
      }


    def getKetWords = {
        inDB{


          /*  for (i <- from(testdb.MESSAGES)(s => select(s.ID)) ) {
                postToWords(i)

            }   */
            for (i <- from(testdb.MESSAGES)(s => select(s.username)).distinct.drop(5) ;if(i.length < 255)){
                userwords(i)
            }
        }
    }

    def similarity (name1:String,name2:String)={
        val stat1 = from(asd.KeyWords3)(s => where(s.username === name1)select(s))
        val stat2 = from(asd.KeyWords3)(s => where(s.username === name2)select(s))
        val words1 = stat1.map(a => (a.value,a.frequency)).toMap
        val total1 = words1.toSeq.map(_.frequency).sum
        val total2 = words2.toSeq.map(_.frequency).sum
        val words2 = stat2.map(a => (a.value,a.frequency)).toMap
        for {a <- words1
        if(words2.contains(a))}{
            val r1 = words1[a]*1.0/total1
            val r2 = words2[a]*1.0/total2
            val prob = java.m
        }
    }
}


object asd extends Schema {
//   val fdhfgjy = table[Word]("WORDS2")
   // val USERS2 = table[User]("USERS2")
    //val KeyWords2 = table[Word]("KeyWords2")
     val KeyWords3 = table[UserWord]("KeyWords3")
    override def drop = super.drop
}